console.log(Math.max(1, 10))

let weight = [0, 2, 3, 4, 5, 6, 7]; //商品的体积2、3、4、5
let value = [0, 3, 4, 5, 6, 7, 8]; //商品的价值3、4、5、6
let bagV = 12; //背包大小
let item = [];


let list = [];
for (let i = 0; i < weight.length; i++) {
    list[i] = [];
    for (let j = 0; j <= bagV; j++) {
        list[i][j] = 0;
    }
}
console.log(list)
findMax();
findWhat(weight.length - 1, bagV);
console.log(list)
console.log(item)

let result_tr = document.querySelector(".result thead tr");
for (let i = 0; i <= bagV; i++) {
    let th = document.createElement("th");
    th.innerHTML = i;
    result_tr.appendChild(th);
}
let result_tbody = document.querySelector(".result tbody");
for (let i = 0; i < list.length; i++) {
    let tr = document.createElement("tr")
    let td = document.createElement("td");
    td.innerHTML = i;
    tr.appendChild(td);
    for (let j = 0; j < list[i].length; j++) {
        let td = document.createElement("td");
        td.innerHTML = list[i][j];
        tr.appendChild(td);

    }
    result_tbody.appendChild(tr);
}

//最优解情况
function findMax() { //动态规划
    for (let i = 1; i < weight.length; i++) {
        for (let j = 1; j <= bagV; j++) {
            if (j < weight[i]) {
                list[i][j] = list[i - 1][j];
            } else {
                list[i][j] = Math.max(list[i - 1][j], list[i - 1][j - weight[i]] + value[i]);
            }
        }
    }
}

function findWhat(i, j) { //最优解情况
    console.log(i + "" + j)
    if (i > 0) {
        if (list[i][j] == list[i - 1][j]) {
            item[i] = 0;
            findWhat(i - 1, j);
        } else if (j - weight[i] >= 0 && list[i][j] == list[i - 1][j - weight[i]] + value[i]) {
            item[i] = 1;
            findWhat(i - 1, j - weight[i]);
        }
    }
}

function indexreturn() {
    let data = [];
    for (let i = 1; i <= item.length; i++) {
        data.push(i);
    }
    return data;
}


function creatFirstEchart() {
    let firstEchart = echarts.init(document.querySelector("#first-echart"));
    let categoryData = indexreturn();
    let chartdata = item;
    let itemcolor = {
        type: 'linear',
        colorStops: [{
                offset: 0,
                color: 'rgba(6, 120, 157,1)',
            },
            {
                offset: 0.5,
                color: 'rgba(6, 120, 157,0.2)',
            },
            {
                offset: 1,
                color: 'rgba(6, 120, 157,1)',
            },
        ],
    };

    option = {
        toolbox: {
            show: false,
            feature: {
                saveAsImage: {
                    backgroundColor: '#031245',
                },
                restore: {},
            },
            iconStyle: {
                borderColor: '#c0c3cd',
            },
        },

        color: ['#00D7E9', 'rgba(0, 215, 233, 0.9)'],
        grid: {
            containLabel: false,
            left: 40,
            right: 20,
            bottom: 40,
            top: 40,
        },
        xAxis: {
            nameTextStyle: {
                color: '#c0c3cd',
                padding: [0, 0, -10, 0],
                fontSize: 14,
            },
            axisLabel: {
                color: '#c0c3cd',
                fontSize: 14,
                interval: 0,
            },
            axisLine: {
                lineStyle: {
                    color: '#335971',
                },
                show: true,
            },
            data: categoryData,
            type: 'category',
        },
        yAxis: {
            nameTextStyle: {
                color: '#c0c3cd',
                padding: [0, 0, -10, 0],
                fontSize: 14,
            },
            axisLabel: {
                color: '#c0c3cd',
                fontSize: 14,
            },
            axisTick: {
                lineStyle: {
                    color: '#668092',
                    width: 1,
                },
                show: true,
            },
            splitLine: {
                show: true,
                lineStyle: {
                    color: '#335971',
                    // "type": "dashed"
                },
            },
            axisLine: {
                lineStyle: {
                    color: '#668092',
                    width: 1,
                    // "type": "dashed"
                },
                show: true,
            },
            name: '',
        },
        series: [{
                data: chartdata,
                type: 'bar',
                barMaxWidth: 'auto',
                barWidth: 20,
                itemStyle: {
                    color: {
                        x: 0,
                        y: 1,
                        x2: 0,
                        y2: 0,
                        type: 'linear',
                        colorStops: [{
                                offset: 0,
                                color: '#00D7E9',
                            },
                            {
                                offset: 1,
                                color: 'rgba(0, 167, 233,0.3)',
                            },
                        ],
                    },
                },
                label: {
                    show: true,
                    position: 'top',
                    distance: 10,
                    color: '#fff',
                },
            },
            {
                data: [1, 1, 1, 1, 1, 1],
                type: 'pictorialBar',
                barMaxWidth: '20',
                symbol: 'diamond',
                symbolOffset: [0, '50%'],
                symbolSize: [20, 10],
            },
            {
                data: chartdata,
                type: 'pictorialBar',
                barMaxWidth: '20',
                symbolPosition: 'end',
                symbol: 'diamond',
                symbolOffset: [0, '-50%'],
                symbolSize: [20, 12],
                zlevel: 2,
            },
        ],
        tooltip: {
            show: false,
        },
    };
    firstEchart.setOption(option)
    window.onresize = function() {
        firstEchart.resize();
    };
}

creatFirstEchart();